package core

import (
	"github.com/sirupsen/logrus"
	"github.com/smartwalle/alipay/v3"
	"muziai-server/global"
)

func InitAlipayClient() (client *alipay.Client, err error) {
	var Client *alipay.Client
	var kappid = global.Config.Aalipay.KAppID
	var kprivatekey = global.Config.Aalipay.KPrivateKey
	// 初始化 Alipay Client
	Client, err = alipay.New(kappid, kprivatekey, false)
	if err != nil {
		logrus.Error("初始化支付宝失败: %v", err)
		return nil, err
	}

	// 加载证书
	if err = Client.LoadAppCertPublicKeyFromFile("alicert/appPublicCert.crt"); err != nil {
		logrus.Error("加载应用公钥证书失败: %v", err)
		return nil, err
	}
	if err = Client.LoadAliPayRootCertFromFile("alicert/alipayRootCert.crt"); err != nil {
		logrus.Error("加载支付宝根证书失败: %v", err)
		return nil, err
	}
	if err = Client.LoadAlipayCertPublicKeyFromFile("alicert/alipayPublicCert.crt"); err != nil {
		logrus.Error("加载支付宝公钥证书失败: %v", err)
		return nil, err
	}
	return Client, nil
}
